<?php

abstract class CApplication extends CModule
{
	public $name='My Applicatioin';
	public $charset='UTF-8';
	public $sourceLanguage='en_us';
	private $_id;
	private $_basePath;
	private $_runtimePath;
	private $_extensionPaht;
	private $_globalState;
	private $_stateChanged;
	private $ended=false;
	private $_language;
	private $_homeUrl;
	
	public function __construct($config=null)
	{
		Yii::setApplication($this);
		if(is_string($config))
		{
			$config=require($config);
		}
		if(isset($config['basePath']))
		{
			$this->setBasePath($config['basePath']);
			unset($config['basePath']);
		}
		else 
		{
			$this->setBasePath('protected');
		}
		
		Yii::setPathOfAlias('application',$this->getBasePath());
		Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
		Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
		
		$this->preinit();
		
		$this->initSystemHandlers();
		$this->registerCoreComponents();
		$this->configure($config);
		$this->attachBehaviors($this->behaviors);
		$this->preloadComponents();
		
		$this->init();
	}
	
	public function setBasePath($path)
	{
		if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
		{
			throw new CException('application base path is not a valid directory');
		}
	}
	
	public function getBasePath()
	{
		return $this->_basePath;
	}
	
	protected function initSystemHandlers()
	{
		if(YII_ENABLE_EXCEPTION_HANDLER)
		{
			set_exception_handler(array($this,'handlerException'));
		}
		
		if(YII_ENABLE_ERROR_HANDLER)
		{
			set_error_handler(array($this,'handleError'),error_reporting());
		}
	}
	
	protected function registerCoreComponents()
	{
		$components=array(
			'coreMessages'=>array(
				'class'=>'CPhpMessageSource',
				'language'=>'en_us',
				'basePath'=>YII_PATH.DIRECTORY_SEPARATOR.'messages',
			),
			'db'=>array(
				'class'=>'CDbConnection',
			),
			'messages'=>array(
				'class'=>'CPhpMessageSource',
			),
			'errorHandler'=>array(
				'class'=>'CErrorHandler',
			),
			'securityManager'=>array(
				'class'=>'CSecurityManager',
			),
			'statePersister'=>array(
				'class'=>'CStatePersister',
			),
			'urlManager'=>array(
				'class'=>'CUrlManager',
			),
			'request'=>array(
				'class'=>'CHttpRequest',
			),
			'format'=>array(
				'class'=>'CFormatter',
			),
		);
		$this->setComonents($components);
	}
	
	
	
}